import axios from 'axios';
import { useQuasar } from 'quasar';
import { get_access_token, is_token_valid } from './token';
import { ElMessage } from 'element-plus';
import { route } from 'quasar/wrappers';
export const baseURL = process.env.APP_API_BASE_URL;
// export const baseURL = 'http://localhost:9001/api';
// export const baseURL = 'http://192.168.2.3:9001/api';
const api = axios.create({ baseURL });

const authedApi = axios.create({ baseURL });

authedApi.interceptors.request.use((config) => {
  try {
    const at = get_access_token();
    config.headers['Authorization'] = 'Token ' + at;
    return Promise.resolve(config);
    // return config
  } catch (e) {}
  localStorage.clear();
  return Promise.reject();
  // signout();
});

authedApi.interceptors.response.use(
  (response) => {
    // You have to return the response here or you won't have access to it
    // later
    return response;
  },
  (error) => {
    if (error.response.status == 401) {
      // localStorage.clear();
      // window.location.replace('/login');
      return Promise.reject(error);
    } else {
      // 训练任务详情专门提示，后期需要优化
      if (error.response.config.url.includes('output.log') == false) {
        let str: any;
        if (error.response.status == 400) {
          if (error.response.data.hasOwnProperty('message')) {
            str = error.response.data.message;
          }
          if (error.response.data.hasOwnProperty('labels')) {
            str = error.response.data.labels[0];
          }
          if (error.response.data.hasOwnProperty('title')) {
            str = error.response.data.title.message;
          }
          if (error.response.data.hasOwnProperty('constructor')) {
            str = `${
              error.response.data.constructor == Array
                ? error.response.data[0]
                : error.response.data.labels[0]
            }`;
          }
          // str = error.response.data.message
        } else {
          str = statusText(error.response.status);
        }
        if (error.response.status == 403) {
          if (error.response.data.hasOwnProperty('detail')) {
            if (
              error.response.data.detail ==
              'You do not have permission to perform this action.'
            ) {
              str = '没有权限执行该操作';
            }
          }
          if (error.response.data.hasOwnProperty('message')) {
            str = error.response.data.message
          }
        }

        ElMessage.error(str);
      }
    }
    // if (error.response.status >= 500) {
    //   console.log(error.response);

    //   ElMessage({
    //     showClose: true,
    //     message: `error status ${error.response.status}`,
    //     type: 'error',
    //   });
    // }
    // else if (error.response.status >= 400) {
    //   ElMessage({
    //     showClose: true,
    //     message: `error ${error.response.status}
    //     ${error.response.data.constructor == Array ? error.response.data : error.response.data.labels}`,
    //     type: 'error',
    //   });
    // }
    // should reject the promise so your api call goes to catch block
    return Promise.reject(error);
  }
);
// 错误状态码提示
const statusText = (num: number) => {
  switch (num) {
    case 400: {
      return '数据格式有误';
    }
    case 403: {
      return '服务器拒绝请求';
    }
    case 404: {
      return 'API请求出错';
    }
    case 500:
    case 502:
    case 503: {
      return '服务器错误';
    }
    case 504: {
      return '请求超时';
    }
  }
};

export { api, authedApi };
